﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class TinhThanhDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_TinhThanh> GetAllTinhThanh()
        {
            return DBDanhMuc.dm_TinhThanhs.Where(p => p.IsDelete == false).ToList();
        }
        public dm_TinhThanh AddNewTinhThanh(dm_TinhThanh TinhThanh)
        {
            DBDanhMuc.dm_TinhThanhs.InsertOnSubmit(TinhThanh);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteTinhThanh(int TinhThanhID)
        {
            dm_TinhThanh TinhThanhNew =
                DBDanhMuc.dm_TinhThanhs.Single(p => p.Id == TinhThanhID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_TinhThanhs.DeleteOnSubmit(TinhThanhNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteTinhThanh(dm_TinhThanh TinhThanh)
        {
            dm_TinhThanh TinhThanhDelete =
                DBDanhMuc.dm_TinhThanhs.Single(p => p.Id == TinhThanh.Id);
            if (TinhThanhDelete.hs_NhanViens.Count() == 0)
            {
                TinhThanhDelete.Id = TinhThanh.Id;
                TinhThanhDelete.UpdateDate = DateTime.Now;
                TinhThanhDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                TinhThanhDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdateTinhThanh(dm_TinhThanh TinhThanh)
        {
            dm_TinhThanh TinhThanhUpdate =
                DBDanhMuc.dm_TinhThanhs.Single(p => p.Id == TinhThanh.Id);

            TinhThanhUpdate.Id = TinhThanh.Id;
            TinhThanhUpdate.TenTinh = TinhThanh.TenTinh;
            TinhThanhUpdate.UpdateDate = TinhThanhUpdate.UpdateDate;
            TinhThanhUpdate.UpdateBy = TinhThanhUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_TinhThanh> SearchTinhThanhPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_TinhThanh>)DBDanhMuc.dm_TinhThanhs.Where(p => p.TenTinh.Contains(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_TinhThanh GetTinhThanhById(string id)
        {
            try
            {
                dm_TinhThanh item = this.DBDanhMuc.dm_TinhThanhs.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_TinhThanh GetTinhThanhByTenCheck(string ten)
        {

            IEnumerable<dm_TinhThanh> list = this.DBDanhMuc.dm_TinhThanhs.Where(p => p.TenTinh.Equals(ten) && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_TinhThanh GetTinhThanhByTen(string ten)
        {

            IEnumerable<dm_TinhThanh> list = this.DBDanhMuc.dm_TinhThanhs.Where(p => p.TenTinh.Equals(ten));
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_TinhThanh> RecycleGetTinhThanhPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TinhThanhs.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_TinhThanh> GetTinhThanhPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TinhThanhs.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_TinhThanh BuildNewTinhThanh(TinhThanhForm form)
        {
            dm_TinhThanh newTinhThanh = new dm_TinhThanh();
            newTinhThanh.TenTinh = form.TenTinh;
            newTinhThanh.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newTinhThanh.CreateDate = DateTime.Now;
            newTinhThanh.IsDelete = false;
            return newTinhThanh;
        }
        public int PhucHoiTinhThanh(int id)
        {
            dm_TinhThanh phuchoi = new dm_TinhThanh();
            phuchoi = this.GetTinhThanhById(Convert.ToString(id));
            dm_TinhThanh item = new dm_TinhThanh();
            item = this.GetTinhThanhByTenCheck(phuchoi.TenTinh);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_TinhThanh BuildEditTinhThanh(TinhThanhForm form)
        {
            dm_TinhThanh newTinhThanh = new dm_TinhThanh();
            newTinhThanh.Id = form.id;
            newTinhThanh.TenTinh = form.TenTinh;
            newTinhThanh.UpdateDate = DateTime.Now;
            newTinhThanh.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newTinhThanh;
        }
        public dm_TinhThanh GetLoaiCongChucIdMax()
        {
            IList<dm_TinhThanh> list = DBDanhMuc.dm_TinhThanhs.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public TinhThanhForm ConvertToForm(dm_TinhThanh obj)
        {
            TinhThanhForm form = new TinhThanhForm();
            form.id = obj.Id;
            form.TenTinh = obj.TenTinh;
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}